package com.qingke.besuper.command;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import com.qingke.besuper.command.impl.AnswerCommand;
import com.qingke.besuper.command.impl.AskCommand;
import com.qingke.besuper.command.impl.ChooseBestAnswerCommand;
import com.qingke.besuper.command.impl.HelpUserCommand;
import com.qingke.besuper.command.impl.ListACommand;
import com.qingke.besuper.command.impl.ListCommand;
import com.qingke.besuper.command.impl.ListPCommand;
import com.qingke.besuper.command.impl.ListQCommand;
import com.qingke.besuper.command.impl.ScoreCommand;
import com.qingke.besuper.command.impl.UpdateQuestionToCloseCommand;
import com.qingke.besuper.command.impl.UpdateQuestionToOpenCommand;
import com.qingke.besuper.pojo.User;

public class UserCommandFactory extends SystemCommandFactory{
	public static Map<CommandCode,Class<? extends UserCommand>> userMap = new HashMap<>();
	static {
		userMap.put(CommandCode.LIST, ListCommand.class);
		userMap.put(CommandCode.LIST_P, ListPCommand.class);
		userMap.put(CommandCode.LIST_Q, ListQCommand.class);
		userMap.put(CommandCode.LIST_A, ListACommand.class);
		userMap.put(CommandCode.ASK, AskCommand.class);
		userMap.put(CommandCode.ANSWER, AnswerCommand.class);
		userMap.put(CommandCode.SCORE, ScoreCommand.class);
		userMap.put(CommandCode.OPEN, UpdateQuestionToOpenCommand.class);
		userMap.put(CommandCode.CLOSE, UpdateQuestionToCloseCommand.class);
		userMap.put(CommandCode.BEST, ChooseBestAnswerCommand.class);
		userMap.put(CommandCode.HELP, HelpUserCommand.class);
	}
	private User user;
	public UserCommandFactory(User user){
		this.user=user;
	}
	public Command buildCommand(CommandCode cmdCode){
		UserCommand command=null;
		Class<?> commandClass=null;
		commandClass=userMap.get(cmdCode);
		
		if(commandClass == null){
			return super.buildCommand(cmdCode);
		}
		
		try {
//			command=(UserCommand) commandClass.getConstructors()[0].newInstance(user);
			command=(UserCommand) commandClass.getConstructor(User.class).newInstance(user);
		} catch (IllegalArgumentException | InvocationTargetException | SecurityException | InstantiationException | IllegalAccessException | NoSuchMethodException e) {
			e.printStackTrace();
		}
		
		return command;
	}
}
